package com.conwax.gmap;

import org.apache.wicket.Application;
import org.apache.wicket.Page;
import org.apache.wicket.Request;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.WicketAjaxReference;
import org.apache.wicket.behavior.AbstractAjaxBehavior;
import org.apache.wicket.behavior.IBehavior;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.WicketEventReference;
import org.apache.wicket.markup.html.resources.JavascriptResourceReference;
import org.apache.wicket.protocol.http.WebApplication;

public class MapsApiBehavior extends AbstractAjaxBehavior
{

	private static final long serialVersionUID = 1L;

	protected static final String GOOGLE_LOAD_MAPS = "google.load(\"maps\", \"2.x\");";

	protected static final ResourceReference WICKET_GMAP_JS = new JavascriptResourceReference(
			MapsApiBehavior.class, "conwax-gmap.js");

	// private Set<GMapJSElement> gMapBehaviors = new HashSet<GMapJSElement>();
	//
	// private final GEvent gEvent;
	//
	public MapsApiBehavior()
	{
		// this.gEvent = new GEvent();
	}

	@Override
	public void onBind()
	{
		super.onBind();
		if (!(getComponent() instanceof Page))
		{
			throw new Error(this.getClass().getName() + " may only be bound to a Page, not to a "
					+ getComponent().toString());
		}
		for (IBehavior behavior : getComponent().getBehaviors())
		{
			if (behavior != this && behavior instanceof MapsApiBehavior)
			{
				throw new Error("There may only be one " + this.getClass().getName());
			}
		}
	}


	@Override
	public void renderHead(IHeaderResponse response)
	{
		super.renderHead(response);
		response
				.renderJavascript(GOOGLE_LOAD_MAPS, MapsApiBehavior.class.getName() + "_googleload");
		response.renderJavascriptReference(WicketEventReference.INSTANCE);
		response.renderJavascriptReference(WicketAjaxReference.INSTANCE);

		if (Application.get().getDebugSettings().isAjaxDebugModeEnabled())
		{
			response.renderJavascriptReference(new JavascriptResourceReference(
					AbstractDefaultAjaxBehavior.class, "wicket-ajax-debug.js"));
			response.renderJavascript("wicketAjaxDebugEnable=true;", "wicket-ajax-debug-enable");
		}


		response.renderJavascriptReference(WICKET_GMAP_JS);
		// see:
		// http://www.google.com/apis/maps/documentation/#Memory_Leaks
		response.renderOnEventJavascript("window", "onUnload", "google.maps.Unload();");
	}

	// public boolean add(GMapJSElement gMapBehavior) {
	// if (gMapBehavior == null) {
	// throw new IllegalArgumentException("Argument may not be null");
	// }
	// return gMapBehaviors.add(gMapBehavior);
	// }

	// public boolean remove(GMapJSElement gMapBehavior) {
	// if (gMapBehavior == null) {
	// throw new IllegalArgumentException("Argument may not be null");
	// }
	// return gMapBehaviors.remove(gMapBehavior);
	// }

	/**
	 * @see org.apache.wicket.behavior.IBehaviorListener#onRequest()
	 */
	public final void onRequest()
	{
		WebApplication app = (WebApplication)getComponent().getApplication();
		AjaxRequestTarget target = app.newAjaxRequestTarget(getComponent().getPage());
		RequestCycle.get().setRequestTarget(target);
		onEvent(target);
	}

	/**
	 * @param target
	 *            The AJAX target
	 */
	protected void onEvent(AjaxRequestTarget target)
	{
		Request request = RequestCycle.get().getRequest();
		int listenerId = Integer.parseInt(request.getParameter("listenerId"));
		// gEvent.onEvent(target, listenerId);
	}
}
